package com.dgut.service.impl;

import com.dgut.service.DownLoadService;
import com.dgut.Utils.DownLoadUtils;
import com.dgut.Utils.EXCELUtils;
import com.dgut.Utils.PDFUtils;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

/**
 * @author GooRay
 * 创建于 2020/6/21
 */
@Service
public class DownLoadServiceImpl implements DownLoadService {

    @Override
    public boolean DownLoadPDF(HttpServletRequest request, HttpServletResponse response) {
        try{
            String filename = PDFUtils.DownLoad();
            ServletContext servletContext = request.getServletContext();
            FileInputStream fileInputStream = new FileInputStream(filename);
            //2.使用字节输入流加载进内存
            //获取MIME类型
            String mimeType = servletContext.getMimeType(filename);
            //设置MIME类型
            response.setHeader("content-type",mimeType);
            //设置下载
            //解决中文文件名乱码问题
            String agent = request.getHeader("User-agent");
            filename = DownLoadUtils.getFileName(agent, filename);
            response.setHeader("content-disposition","attachment;filename="+new Date().getTime()+".pdf");
            byte[] bytes = new byte[1024*8];
            ServletOutputStream outputStream = response.getOutputStream();
            int length = 0;
            while((length = fileInputStream.read(bytes))!=-1) {
                outputStream.write(bytes, 0, length);
            }
            fileInputStream.close();
            File file = new File(filename);
            if(file.exists()){
                file.delete();
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean DownLoadEXCEL(HttpServletRequest request, HttpServletResponse response) {
        try{
            String filename = EXCELUtils.DownLoad();
            ServletContext servletContext = request.getServletContext();
            FileInputStream fileInputStream = new FileInputStream(filename);
            //2.使用字节输入流加载进内存
            //获取MIME类型
            String mimeType = servletContext.getMimeType(filename);
            //设置MIME类型
            response.setHeader("content-type",mimeType);
            //设置下载
            //解决中文文件名乱码问题
            String agent = request.getHeader("User-agent");
            filename = DownLoadUtils.getFileName(agent, filename);
            response.setHeader("content-disposition","attachment;filename="+new Date().getTime()+".xls");
            byte[] bytes = new byte[1024*8];
            ServletOutputStream outputStream = response.getOutputStream();
            int length = 0;
            while((length = fileInputStream.read(bytes))!=-1) {
                outputStream.write(bytes, 0, length);
            }
            fileInputStream.close();
            File file = new File(filename);
            if(file.exists()){
                file.delete();
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
